/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.persistence.memory.criteria.criterion;

import com.genia.toolbox.persistence.basis.criteria.BasicCriteriaResult;
import com.genia.toolbox.persistence.basis.manager.AbstractPersistenceManager;
import com.genia.toolbox.persistence.criteria.criterion.Criterion;
import com.genia.toolbox.persistence.exception.PersistenceException;
import com.genia.toolbox.persistence.memory.criteria.MemoryCriteria;

/**
 * This junction represents the dualization of a junction given in argument.
 */
public class MemoryDualJunction
    implements MemoryJunction
{

  /**
   * the implementation of <code>PersistenceManager</code>.
   */
  private final transient AbstractPersistenceManager persistentManager;

  /**
   * the junction we dualize.
   */
  private final transient MemoryJunction baseJunction;



  /**
   * the constructor of this junction.
   * 
   * @param persistentManager
   *          the implementation of <code>PersistenceManager</code>
   * @param baseJunction
   *          the junction we dualize
   */
  public MemoryDualJunction(final AbstractPersistenceManager persistentManager, final MemoryJunction baseJunction)
  {
    this.persistentManager = persistentManager;
    this.baseJunction = baseJunction;
  }



  /**
   * add a <code>Criterion</code> to the list of sub-criterion of this
   * junction.
   * 
   * @param criterion
   *          the sub-criterion to add.
   */
  public void add(final Criterion criterion)
  {
    baseJunction.add(persistentManager.getRestrictionsManager().not(criterion));
  }



  /**
   * return <code>true</code> if the <code>BasicCriteriaResult</code> given
   * in parameter match this <code>Criterion</code> in the
   * <code>Criteria> criteria</code>. That mean that any sub-criterions
   * match. We use the duality between and and or to compute this value.
   * 
   * @param persistenceManager
   *          the implementation of <code>PersistenceManager</code>
   * @param criteria
   *          the criteria this <code>Criterion</code> comes from
   * @param line
   *          the line to test
   * @return if the <code>BasicCriteriaResult</code> given in parameter match
   *         this <code>Criterion</code>.
   * @throws PersistenceException
   *           when a persistence error occurred
   */
  public boolean match(final AbstractPersistenceManager persistenceManager, final MemoryCriteria<?> criteria, final BasicCriteriaResult<?> line)
      throws PersistenceException
  {
    return !baseJunction.match(persistenceManager, criteria, line);
  }



  /**
   * Creates and returns a deep copy of this object.
   * 
   * @return a deep copy of this object
   */
  @Override
  public MemoryDualJunction clone()
  {
    return new MemoryDualJunction(persistentManager, baseJunction.clone());
  }

}
